package com.gxg.controller;

import com.gxg.entities.ExperimentalCheckWork;
import com.gxg.entities.ExperimentalImageUploadResult;
import com.gxg.entities.StudentUser;
import com.gxg.entities.User;
import com.gxg.services.CourseService;
import com.gxg.services.GetEncodingService;
import com.gxg.services.UserService;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class CourseController
{
  @Autowired
  private CourseService courseService;
  @Autowired
  private UserService userService;
  @Autowired
  private GetEncodingService getEncodingService;
  
  @PostMapping({"/create_course"})
  @ResponseBody
  public String createCourse(@RequestParam("courseTap") String courseTap, @RequestParam("courseName") String courseName, @RequestParam("courseDescribe") String courseDescribe, @RequestParam("courseImage") MultipartFile courseImage, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.createCourse(courseTap, courseName, courseDescribe, courseImage, user);
  }
  
  @PostMapping({"/create_experimental_document"})
  @ResponseBody
  public String createExperimentalDocument(@RequestParam("experimentalTitle") String experimentalTitle, @RequestParam("experimentalContent") String experimentalContent, @RequestParam("courseId") String courseId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.createExperimental(experimentalTitle, experimentalContent, courseId, user);
  }
  
  @PostMapping({"/get_my_courses"})
  @ResponseBody
  public String getMyCourses(@RequestParam("page") String page, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.getMyCourses(page, user);
  }
  
  @PostMapping({"/get_course_info"})
  @ResponseBody
  public String getCourseInfo(@RequestParam("courseId") String courseId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getCourseInfoById(courseId);
  }
  
  @PostMapping({"/get_teacher_courses_count"})
  @ResponseBody
  public String getTeacherCoursesCount(@RequestParam("teacherId") String teacherId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getCoursesCountByTeacher(teacherId);
  }
  
  @PostMapping({"/get_my_courses_top_5"})
  @ResponseBody
  public String getMyCoursesTop5(HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.getCoursesTop5ByTeacher(user.getId());
  }
  
  @RequestMapping({"/upload_experimental_img"})
  @ResponseBody
  public ExperimentalImageUploadResult uploadExperimentalImg(@RequestParam("experimentalImage") MultipartFile experimentalImage)
  {
    if (experimentalImage.isEmpty()) {
      System.out.println("kong");
    }
    String result = this.courseService.uploadExperimentalImg(experimentalImage);
    ExperimentalImageUploadResult uploadResult = new ExperimentalImageUploadResult();
    if (result.equals("error"))
    {
      uploadResult.setErrno(1);
      ArrayList<String> arrayList = new ArrayList();
      arrayList.add("error");
      uploadResult.setData(arrayList);
    }
    else
    {
      uploadResult.setErrno(0);
      ArrayList<String> arrayList = new ArrayList();
      arrayList.add(result);
      uploadResult.setData(arrayList);
    }
    return uploadResult;
  }
  
  @PostMapping({"/get_course_experimental"})
  @ResponseBody
  public String getCourseExperimental(@RequestParam("courseId") String courseId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getCourseExperimental(courseId);
  }
  
  @PostMapping({"/get_course_experimental_top5"})
  @ResponseBody
  public String getCourseExperimentalTop5(@RequestParam("courseId") String courseId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getCourseExperimentalTop5(courseId);
  }
  
  @PostMapping({"/get_experimental_information"})
  @ResponseBody
  public String getExperimentalInformation(@RequestParam("experimentalId") String experimentalId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getExperimetalInformation(experimentalId);
  }
  
  @PostMapping({"/upload_experimental_document"})
  @ResponseBody
  public String uploadExperimentalDocument(@PathParam("courseId") String courseId, @PathParam("experimentalDocumentTitle") String experimentalDocumentTitle, @PathParam("experimentalDocument") MultipartFile experimentalDocument, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.uploadExperimentalDocument(courseId, experimentalDocumentTitle, experimentalDocument);
  }
  
  @PostMapping({"/edit_course_information"})
  @ResponseBody
  public String editCourseInformation(@RequestParam("courseId") String courseId, @RequestParam("courseName") String courseName, @RequestParam("courseDescription") String courseDescription, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.editCourseInformation(courseId, courseName, courseDescription);
  }
  
  @PostMapping({"/delete_course"})
  @ResponseBody
  public String deleteCourse(@RequestParam("courseId") String courseId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.deleteCourse(courseId, user);
  }
  
  @PostMapping({"/edit_experimental_document"})
  @ResponseBody
  public String editExperimentalDocument(@PathParam("experimentalId") String experimentalId, @PathParam("experimentalTitle") String experimentalTitle, @PathParam("experimentalContent") String experimentalContent, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.editExperimentalDocument(experimentalId, experimentalTitle, experimentalContent, user);
  }
  
  @PostMapping({"/edit_experimental_document_title"})
  @ResponseBody
  public String editExperimentalDocumentTitle(@RequestParam("experimentalId") String experimentalId, @RequestParam("experimentalTitle") String experimentalTitle, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.editExperimentalDocumentTitle(experimentalId, experimentalTitle, user);
  }
  
  @PostMapping({"/edit_experimental_document_content"})
  @ResponseBody
  public String editExperimentalDocumentContent(@RequestParam("editExperimentalDocumentContentPdfExperimentalId") String editExperimentalDocumentContentPdfExperimentalId, @RequestParam("editExperimentDocumentInput") MultipartFile editExperimentalDocumentInput, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.editExperimentalDocumentContent(editExperimentalDocumentContentPdfExperimentalId, editExperimentalDocumentInput, user);
  }
  
  @PostMapping({"/delete_experimental_document"})
  @ResponseBody
  public String deleteExperimentalDocument(@RequestParam("experimentalId") String experimentalId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.deleteExperimentalDocument(experimentalId, user);
  }
  
  @PostMapping({"/get_courses_top4"})
  @ResponseBody
  public String getCoursesTop4(HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getCoursesTopNumber(4);
  }
  
  @PostMapping({"/get_courses_count_by_tab"})
  @ResponseBody
  public String getCoursesCountByTab(@RequestParam("coursesTab") String[] coursesTab, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getCoursesCountByTab(coursesTab);
  }
  
  @PostMapping({"/show_courses_by_tab_and_page"})
  @ResponseBody
  public String showCoursesByTabAndPage(@RequestParam("tab") String tab, @RequestParam("page") int page, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getCoursesByTabAndPage(tab, page);
  }
  
  @PostMapping({"/get_courses_top5"})
  @ResponseBody
  public String getCoursesTop5(HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getCoursesTopNumber(5);
  }
  
  @PostMapping({"/get_experimental_document"})
  @ResponseBody
  public String getExperimentalDocument(@RequestParam("experimentalId") String experimentalId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getExperimentalDocument(experimentalId);
  }
  
  @PostMapping({"/upload_experimental_environment"})
  @ResponseBody
  public String uploadExperimentalEnvironment(@RequestParam("documentId") String documentId, @RequestParam("experimentalEnvironment") MultipartFile experimentalEnvironment, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.uploadExperimentalEnvironment(user, experimentalEnvironment, documentId);
  }
  
  @PostMapping({"/get_course_experimental_environment"})
  @ResponseBody
  public String getCourseExperimentalEnvironment(@RequestParam("documentId") String documentId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getCourseExperimentalEnvironment(documentId);
  }
  
  @PostMapping({"/send_experimental_environment"})
  @ResponseBody
  public String sendExperimentalEnvironment(@RequestParam("experimentalEnvironmentId") String experimentalEnvironmentId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.sendExperimentalEnvironment(experimentalEnvironmentId, user);
  }
  
  @PostMapping({"/delete_experimental_environment"})
  @ResponseBody
  public String deleteExperimentalEnvironment(@RequestParam("experimentalEnvironmentId") String experimentalEnvironmentId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.deleteExperimentalEnvironment(experimentalEnvironmentId, user);
  }
  
  @PostMapping({"/get_teacher_courses"})
  @ResponseBody
  public String getTeacherCourses(@RequestParam("teacherId") String teacherId, @RequestParam("page") String page, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getTeacherCourses(teacherId, page);
  }
  
  @PostMapping({"/upload_experimental_report"})
  @ResponseBody
  public String uploadExperimentalReport(@RequestParam("uploadExperimentalReportFormExperimentalId") String experimentalId, @RequestParam("uploadExperimentalReportFormExperimentalNameInput") String experimentalReportName, @RequestParam("uploadStudentIdInput") String studentId, @RequestParam("uploadStudentNameInput") String studentName, @RequestParam("uploadExperimentalReportFormInput") MultipartFile experimentalReport, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    System.out.println(studentName);
    System.out.println(this.getEncodingService.getEncoding(studentName));
    return this.courseService.uploadExperimentalReport(experimentalId, experimentalReportName, experimentalReport, studentId, studentName);
  }
  
  @PostMapping({"/get_experimental_report"})
  @ResponseBody
  public String getExperimentalReport(@RequestParam("experimentalId") String experimentalId, @RequestParam("page") String page, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.getExperimentalReport(experimentalId, page, user);
  }
  
  @PostMapping({"/get_experimental_report_top5"})
  @ResponseBody
  public String getExperimentalReportTop5(@RequestParam("experimentalId") String experimentalId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getExperimentalReportTop5(experimentalId);
  }
  
  @PostMapping({"/get_experimental_report_information"})
  @ResponseBody
  public String getExperimentalReportInformation(@RequestParam("experimentalReportId") String experimentalReportId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.getExperimentalReportInformation(experimentalReportId, user);
  }
  
  @PostMapping({"/delete_experimental_report"})
  @ResponseBody
  public String deleteExperimentalReport(@RequestParam("experimentalReportId") String experimentalReportId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.courseService.deleteExperimentalReport(experimentalReportId, user);
  }
  
  @PostMapping({"/upload_experimental_report_score"})
  @ResponseBody
  public String uploadExperimentalReportScore(@RequestParam("SCORE") String score, @RequestParam("comment") String comment, @RequestParam("Id") String Id)
  {
    System.out.println(Id);
    return this.courseService.uploadExperimentalReportScore(Id, score);
  }
  
  @PostMapping({"/get_experimental_score"})
  @ResponseBody
  public String getExperimentalScore(@RequestParam("experimentalId") String experimentalId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    System.out.println(experimentalId);
    return this.courseService.getExperimentalScore(experimentalId);
  }
  
  @PostMapping({"/get_check_work_data"})
  @ResponseBody
  public String getExperimentalCheckWorkData(@RequestParam("mouth") String mouth, @RequestParam("year") String year, @RequestParam("courseId") String courseId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.getCheckWorkDate(mouth, year, courseId);
  }
  
  @PostMapping({"/upload_check_work"})
  @ResponseBody
  public String uploadExperimentalCheckWork(@RequestParam("studentId") String studentId, @RequestParam("studentName") String studentName, @RequestParam("year") String year, @RequestParam("mouth") String mouth, @RequestParam("day") String day, @RequestParam("courseId") String courseId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    ExperimentalCheckWork experimentalCheckWork = new ExperimentalCheckWork();
    experimentalCheckWork.setCourseId(courseId);
    experimentalCheckWork.setStudentId(studentId);
    experimentalCheckWork.setStudentName(studentName);
    experimentalCheckWork.setSignInTime(year + "-" + mouth + "-" + day);
    
    return this.courseService.uploadExperimentalCheckWork(experimentalCheckWork);
  }
  
  @PostMapping({"/take_socre_to_excel"})
  @ResponseBody
  public String takeScoreToExcel(@RequestParam("courseId") String courseId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.courseService.takeScoreToExcel(courseId);
  }
  
  @PostMapping({"/upload_excel_file"})
  @ResponseBody
  public Map<String, Object> uploadExcelFile(MultipartFile file, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    Map<String, Object> map = new HashMap();
    if (session.getAttribute("user") == null)
    {
      map.put("code", Integer.valueOf(1));
      map.put("msg", "没有登录");
    }
    else
    {
      map.put("code", Integer.valueOf(0));
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
      String teacherName = user.getName();
      List<StudentUser> studentUserList = this.courseService.parseXSSFExcelToStudentUser(file, teacherName);
      if ((studentUserList == null) || (studentUserList.isEmpty())) {
        map.put("msg", "文件格式出错!");
      } else {
        for (int i = 0; i < studentUserList.size(); i++)
        {
          String msg = this.userService.addStudentUser((StudentUser)studentUserList.get(i));
          map.put("msg", msg);
        }
      }
    }
    return map;
  }
}
